const path = require("path");
const time = new Date().getTime()
// chunkhash 不能与 --watch (模块热替换) 同时使用，所以需要将 chunkhash 改为 hash
// 哪个开发环境(dev\test\pre\prod)
const code = process.env.NODE_ENV.split('_')[2]
const isWatch = process.argv.indexOf('--watch') > -1
const isDevelopment = code === 'development'
const params = `?t=${time}`
const whichHash = (isDevelopment || isWatch) ? '' : 'chunk'
const jsName = `js/[name].[${whichHash}hash].js` + params
const outputRoot = `dist/${process.env.TARO_ENV}`
const patterns = [
  {from: 'src/common/components/parser/', to: `${outputRoot}/common/components/parser/`},
  {from: 'src/main/parser/', to: `${outputRoot}/main/parser/`},
  {from: 'src/other/parser/', to: `${outputRoot}/other/parser/`},
  {from: 'src/ext.json', to: `${outputRoot}/ext.json`},
  {from: 'src/sitemap.json', to: `${outputRoot}/sitemap.json`}
]

const plugins = []

if (process.env.TARO_ENV === 'tt') {
  patterns.push({from: "src/tt/package.json", to: `${outputRoot}/package.json`})
  plugins.push(path.resolve(__dirname, "../src/utils/modifyAssets"))
}

if (process.env.TARO_ENV === 'kwai') {
  plugins.push('@tarojs/plugin-platform-kwai')
}

const config = {
  framework: "react",
  projectName: 'wxh5',
  date: '2021-1-30',
  designWidth: 750,
  deviceRatio: {
    '640': 2.34 / 2,
    '750': 1,
    '828': 1.81 / 2
  },
  sourceRoot: 'src',
  outputRoot,
  plugins,
  defineConstants: {
    LOCATION_APIKEY: JSON.stringify('4IDBZ-YHFW4-MZ2UR-DM3ON-G72VS-FNFND')
  },
  copy: {
    patterns,
    options: {
      ignore: ['*.wxml', '*.wxss'] // 忽略全局的
    }
  },
  mini: {
    // commonChunks: ['runtime', 'vendors', 'taro', 'commons'],
    optimizeMainPackage: {
      enable: true
    },
    miniCssExtractPluginOption: {
      ignoreOrder: true
    },
    postcss: {
      autoprefixer: {
        enable: true
      },
      pxtransform: {
        enable: true,
        config: {}
      },
      url: {
        enable: true,
        config: {
          limit: 10240 // 设定转换尺寸上限
        }
      }
    }
  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    assetsPublicPath: '/',
    output: {
      filename: jsName,
      chunkFilename: jsName
    },
    imageUrlLoaderOption: {
      limit: 5000,
      name: 'static/images/[name].[chunkhash].[ext]'
    },
    miniCssExtractPluginOption: {
      filename: 'css/[name].[chunkhash].css',
      chunkFilename: 'css/[name].[chunkhash].css',
    },
    router: {
      mode: 'browser'
    },
    devServer: {
      hot: false,
      host: '0.0.0.0',
      port: 10086,
      proxy: {
        '/proxy': {  // rest服务的path
          target: 'https://test-api.ivcheng.com',  // 服务端地址
          changeOrigin: true,
          pathRewrite: {
            '^/proxy': ''
          }
        }
      }
    },
    postcss: {
      autoprefixer: {
        enable: true,
      }
    }
  }
}

module.exports = function (merge) {
  //哪个平台(vc\hiyogo）
  const p = process.env.NODE_ENV.split('_')[0]
  const pNames = {
    'vc': 'Loading...',
    'kunxin': '一码游石棉',
    'cxl': '程小旅',
    'hiyogo': '嗨游购',
    'kd': '蝌蚪亲子',
    'kontai': ' ',
    'lx': '灵锡文旅',
  }
  const pName = pNames[p]
  //哪个客户端(weapp、h5)
  const env = process.env.NODE_ENV.split('_')[1]
  //接口地址
  let _BASE_URL = '';
  //资源地址
  let _RESOURCE_URL = '';
  //主色
  let mainColor = ''
  //辅色
  let auxiliaryColor = ''
  //积分名称
  let integralName = '积分'
  //分销名称
  let fxName = '分销'
  //我的上面的背景
  let myBk = ''
  //优惠券
  let myyhq = '';
  //红包
  let myhb = '';
  //积分
  let myjf = ''
  //订单全部
  let orderall = '';
  //订单待付款
  let orderdfk = '';
  //订单待使用
  let orderdsy = '';
  //订单已完成
  let orderywc = '';
  if (p === 'vc' || p === 'canary') {//如果是微程
    if (code === 'prepare') {
      _BASE_URL = 'https://pre-api.ivcheng.com/'
    } else if (code === 'production') {
      if (p === 'canary') {
        _BASE_URL = 'https://canary-api.ivcheng.com/'
      } else {
        _BASE_URL = 'https://api.ivcheng.com/'
      }
    } else {
      _BASE_URL = 'https://test-api.ivcheng.com/'
    }
    _RESOURCE_URL = 'https://resource.ivcheng.com'
    mainColor = '#ffa000';
    auxiliaryColor = '#ffa000';
    myBk = _RESOURCE_URL + '/resources/images/my_bg.png'
    //优惠券
    myyhq = _RESOURCE_URL + '/resources/images/27f67983-44b1-4990-b6fa-a0e7c0e47667.jpg';
    //红包
    myhb = _RESOURCE_URL + '/resources/images/65f78124-f9cd-48f5-863d-2ca72fdf4451.jpg';
    //积分
    myjf = _RESOURCE_URL + '/resources/images/bcc205d9-8d1d-48f0-adc5-f783e171e1f7.jpg'
    //订单全部
    orderall = _RESOURCE_URL + '/resources/images/48bc3bcf-ee71-4214-a0e3-eb510873b3b0.jpg';
    //订单待付款
    orderdfk = _RESOURCE_URL + '/resources/images/53503379-df97-49f7-bd93-d1eb75aba95c.jpg';
    //订单待使用
    orderdsy = _RESOURCE_URL + '/resources/images/5870eaff-c132-4cce-9af5-f7618d8b7abd.jpg';
    //订单已完成
    orderywc = _RESOURCE_URL + '/resources/images/a82019f9-d3c7-402c-aad2-33020ffdaaa2.jpg';
  } else if (p === 'kunxin') {
    _BASE_URL = 'https://kunxin-api.ivcheng.com/'
    _RESOURCE_URL = 'https://kunxin-resource.ivcheng.com'
    mainColor = '#38ba65';
    auxiliaryColor = '#77d367';
    myBk = _RESOURCE_URL + '/resources/images/my_bg.png'
    //优惠券
    myyhq = _RESOURCE_URL + '/resources/images/27f67983-44b1-4990-b6fa-a0e7c0e47667.jpg';
    //红包
    myhb = _RESOURCE_URL + '/resources/images/65f78124-f9cd-48f5-863d-2ca72fdf4451.jpg';
    //积分
    myjf = _RESOURCE_URL + '/resources/images/bcc205d9-8d1d-48f0-adc5-f783e171e1f7.jpg'
    //订单全部
    orderall = 'https://resource.ivcheng.com/upload/images/48bc3bcf-ee71-4214-a0e3-eb510873b3b0.jpg';
    //订单待付款
    orderdfk = 'https://resource.ivcheng.com/upload/images/53503379-df97-49f7-bd93-d1eb75aba95c.jpg';
    //订单待使用
    orderdsy = 'https://resource.ivcheng.com/upload/images/5870eaff-c132-4cce-9af5-f7618d8b7abd.jpg';
    //订单已完成
    orderywc = 'https://resource.ivcheng.com/upload/images/a82019f9-d3c7-402c-aad2-33020ffdaaa2.jpg';
  } else if (p === 'lx') {//如果是灵锡
    if (code === 'prepare') {
      _BASE_URL = 'https://wenlv-api.lx-internet.com/'
    } else if (code === 'production') {
      _BASE_URL = 'https://wenlv-api.lx-internet.com/'
    } else {
      _BASE_URL = 'https://wenlv-api-test.lx-internet.com/'
    }
    _RESOURCE_URL = 'https://wenlv-resource.lx-internet.com'
    mainColor = '#622ddd';
    auxiliaryColor = '#f1a43a';
    myBk = _RESOURCE_URL + '/resources/images/my_bg.png'
    //优惠券
    myyhq = _RESOURCE_URL + '/resources/images/27f67983-44b1-4990-b6fa-a0e7c0e47667.jpg';
    //红包
    myhb = _RESOURCE_URL + '/resources/images/65f78124-f9cd-48f5-863d-2ca72fdf4451.jpg';
    //积分
    myjf = _RESOURCE_URL + '/resources/images/bcc205d9-8d1d-48f0-adc5-f783e171e1f7.jpg'
    //订单全部
    orderall = _RESOURCE_URL + '/resources/images/48bc3bcf-ee71-4214-a0e3-eb510873b3b0.jpg';
    //订单待付款
    orderdfk = _RESOURCE_URL + '/resources/images/53503379-df97-49f7-bd93-d1eb75aba95c.jpg';
    //订单待使用
    orderdsy = _RESOURCE_URL + '/resources/images/5870eaff-c132-4cce-9af5-f7618d8b7abd.jpg';
    //订单已完成
    orderywc = _RESOURCE_URL + '/resources/images/a82019f9-d3c7-402c-aad2-33020ffdaaa2.jpg';
  } else if (p === 'cxl') {//如果是程小旅
    _BASE_URL = 'https://api-cxl.ivcheng.com/'
    _RESOURCE_URL = 'https://res-cxl.ivcheng.com'
    mainColor = '#ffa000';
    auxiliaryColor = '#ffa000';
    myBk = _RESOURCE_URL + '/resources/images/my_bg.png'
    //优惠券
    myyhq = _RESOURCE_URL + '/resources/images/27f67983-44b1-4990-b6fa-a0e7c0e47667.jpg';
    //红包
    myhb = _RESOURCE_URL + '/resources/images/65f78124-f9cd-48f5-863d-2ca72fdf4451.jpg';
    //积分
    myjf = _RESOURCE_URL + '/resources/images/bcc205d9-8d1d-48f0-adc5-f783e171e1f7.jpg'
    //订单全部
    orderall = _RESOURCE_URL + '/resources/images/48bc3bcf-ee71-4214-a0e3-eb510873b3b0.jpg';
    //订单待付款
    orderdfk = _RESOURCE_URL + '/resources/images/53503379-df97-49f7-bd93-d1eb75aba95c.jpg';
    //订单待使用
    orderdsy = _RESOURCE_URL + '/resources/images/5870eaff-c132-4cce-9af5-f7618d8b7abd.jpg';
    //订单已完成
    orderywc = _RESOURCE_URL + '/resources/images/a82019f9-d3c7-402c-aad2-33020ffdaaa2.jpg';
  } else if (p === 'hiyogo') {//如果是嗨游购
    if (code === 'production') {
      _BASE_URL = 'https://api.hiyogo.cn/'
    } else {
      _BASE_URL = 'https://api.hiyogo.cn/'
    }
    _RESOURCE_URL = 'https://resource.hiyogo.cn'
    mainColor = '#ffa000';
    auxiliaryColor = '#ffa000';
    myBk = _RESOURCE_URL + '/resources/images/my_bg.png'
    //优惠券
    myyhq = _RESOURCE_URL + '/resources/images/27f67983-44b1-4990-b6fa-a0e7c0e47667.jpg';
    //红包
    myhb = _RESOURCE_URL + '/resources/images/65f78124-f9cd-48f5-863d-2ca72fdf4451.jpg';
    //积分
    myjf = _RESOURCE_URL + '/resources/images/bcc205d9-8d1d-48f0-adc5-f783e171e1f7.jpg'
    //订单全部
    orderall = _RESOURCE_URL + '/resources/images/48bc3bcf-ee71-4214-a0e3-eb510873b3b0.jpg';
    //订单待付款
    orderdfk = _RESOURCE_URL + '/resources/images/53503379-df97-49f7-bd93-d1eb75aba95c.jpg';
    //订单待使用
    orderdsy = _RESOURCE_URL + '/resources/images/5870eaff-c132-4cce-9af5-f7618d8b7abd.jpg';
    //订单已完成
    orderywc = _RESOURCE_URL + '/resources/images/a82019f9-d3c7-402c-aad2-33020ffdaaa2.jpg';
  } else if (p === 'kd') {//如果是蝌蚪
    if (code === 'production') {
      _BASE_URL = 'https://api.kedouqinzi.com/'
    } else {
      _BASE_URL = 'https://test-api-kd.ivcheng.com/'
    }
    _RESOURCE_URL = 'https://resource.kedouqinzi.com'
    mainColor = '#38ba65';
    auxiliaryColor = '#77d367';
    integralName = '蝌豆'
    fxName = '达人'
    myBk = 'https://resource.ivcheng.com/resources/weapp/images/202108/my_bg.png'
    //优惠券
    myyhq = 'https://resource.ivcheng.com/upload/images/27f67983-44b1-4990-b6fa-a0e7c0e47667.jpg';
    //红包
    myhb = 'https://resource.ivcheng.com/upload/images/65f78124-f9cd-48f5-863d-2ca72fdf4451.jpg';
    //积分
    myjf = 'https://resource.ivcheng.com/upload/images/bcc205d9-8d1d-48f0-adc5-f783e171e1f7.jpg'
    //订单全部
    orderall = 'https://resource.ivcheng.com/upload/images/48bc3bcf-ee71-4214-a0e3-eb510873b3b0.jpg';
    //订单待付款
    orderdfk = 'https://resource.ivcheng.com/upload/images/53503379-df97-49f7-bd93-d1eb75aba95c.jpg';
    //订单待使用
    orderdsy = 'https://resource.ivcheng.com/upload/images/5870eaff-c132-4cce-9af5-f7618d8b7abd.jpg';
    //订单已完成
    orderywc = 'https://resource.ivcheng.com/upload/images/a82019f9-d3c7-402c-aad2-33020ffdaaa2.jpg';
  } else if (p === 'kontai') {
    if (code === 'production') {
      _BASE_URL = 'https://api.kontai.net/'
    } else {
      _BASE_URL = 'https://kontai-test-api.ivcheng.com/'
    }
    _RESOURCE_URL = 'https://resource.kontai.net/'
    mainColor = '#ffa000';
    auxiliaryColor = '#ffa000';
    integralName = '积分'
    myBk = _RESOURCE_URL + '/resources/images/my_bg.png'
    //优惠券
    myyhq = _RESOURCE_URL + '/resources/images/27f67983-44b1-4990-b6fa-a0e7c0e47667.jpg';
    //红包
    myhb = _RESOURCE_URL + '/resources/images/65f78124-f9cd-48f5-863d-2ca72fdf4451.jpg';
    //积分
    myjf = _RESOURCE_URL + '/resources/images/bcc205d9-8d1d-48f0-adc5-f783e171e1f7.jpg'
    //订单全部
    orderall = _RESOURCE_URL + '/resources/images/48bc3bcf-ee71-4214-a0e3-eb510873b3b0.jpg';
    //订单待付款
    orderdfk = _RESOURCE_URL + '/resources/images/53503379-df97-49f7-bd93-d1eb75aba95c.jpg';
    //订单待使用
    orderdsy = _RESOURCE_URL + '/resources/images/5870eaff-c132-4cce-9af5-f7618d8b7abd.jpg';
    //订单已完成
    orderywc = _RESOURCE_URL + '/resources/images/a82019f9-d3c7-402c-aad2-33020ffdaaa2.jpg';
  }

  // 是否区分用户身份  分销商|普通用户
  let DISTINGUISH_IDENTITY = true
  // 是否可使用分享海报功能
  let SHOW_SHARE_POSTER = true
  // 不可使用环境：灵锡 h5 头条小程序
  if ((p === 'lx' && env === 'h5') || env === 'tt') {
    SHOW_SHARE_POSTER = false
    DISTINGUISH_IDENTITY = false
  }

  // 是否显示拼团
  let IS_SHOW_GROUP_ACTIVITY = true
  // 是否显示活码
  let IS_SHOW_LIVING_CODE = true
  if (env === 'tt') {
    IS_SHOW_LIVING_CODE = false
    IS_SHOW_GROUP_ACTIVITY = false
  }

  // 详情页是否显示编号
  let DETAIL_SHOW_NO = true
  // 是否显示会员
  let IS_SHOW_MEMBER = true
  // 是否显示红包
  let IS_SHOW_RED_BAG = true
  // 是否显示积分
  let IS_SHOW_INTEGRAL = true
  // 是否显示优惠券
  let IS_SHOW_COUPON = true
  if (env === 'tt') {
    IS_SHOW_MEMBER = false
    DETAIL_SHOW_NO = false
    IS_SHOW_RED_BAG = false
    IS_SHOW_INTEGRAL = false
    IS_SHOW_COUPON = false
  }

  return merge({
    env: {
      IS_SHOW_RED_BAG,
      IS_SHOW_COUPON,
      DETAIL_SHOW_NO,
      IS_SHOW_MEMBER,
      IS_SHOW_INTEGRAL,
      SHOW_SHARE_POSTER,
      IS_SHOW_LIVING_CODE,
      DISTINGUISH_IDENTITY,
      IS_SHOW_GROUP_ACTIVITY,
      BASE_URL: '"' + _BASE_URL + '"',
      RESOURCE_URL: '"' + _RESOURCE_URL + '"',
      TARO_ENV: '"' + env + '"',
      NODE_ENV: '"' + code + '"',
      P_ENV: '"' + p + '"',
      P_NAME: '"' + pName + '"',
      integralName: '"' + integralName + '"',
      mainColor: '"' + mainColor + '"',
      auxiliaryColor: '"' + auxiliaryColor + '"',
      myBk: '"' + myBk + '"',
      myyhq: '"' + myyhq + '"',
      myhb: '"' + myhb + '"',
      myjf: '"' + myjf + '"',
      orderall: '"' + orderall + '"',
      orderdfk: '"' + orderdfk + '"',
      orderdsy: '"' + orderdsy + '"',
      orderywc: '"' + orderywc + '"',
      fxName: '"' + fxName + '"'
    },
    sass: {
      data: '$mainColor: ' + mainColor + ';$auxiliaryColor:' + auxiliaryColor + ';$color-brand:' + mainColor + ';'
    }
    // eslint-disable-next-line @typescript-eslint/no-var-requires
  }, config, require('./production'))
}
